package com.example.personal.aiagent.user.controller;

import java.util.stream.Collectors;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Null;
import javax.validation.constraints.Size;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.example.personal.aiagent.common.constant.Constants;
import com.example.personal.aiagent.common.model.vo.CommonListResponse;
import com.example.personal.aiagent.common.model.vo.SuccessResponse;
import com.example.personal.aiagent.common.util.RequestParamUtil;
import com.example.personal.aiagent.user.model.dto.UserDTO;
import com.example.personal.aiagent.user.model.vo.UserListResponse;
import com.example.personal.aiagent.user.model.vo.UserRequest;
import com.example.personal.aiagent.user.service.UserService;

import lombok.extern.slf4j.Slf4j;

/**
 * 用户Api
 */
@Controller
@RequestMapping("/user")
@Slf4j
@Validated
public class UserController {

    @Autowired
    private UserService userService;

    @ResponseBody
    @PostMapping("")
    public SuccessResponse add(@RequestBody @Validated UserRequest request) {
        userService.insertUser(UserDTO.voToDTO(request));
        return new SuccessResponse();
    }

    @ResponseBody
    @PutMapping("/{id}")
    public SuccessResponse modify(@PathVariable int id, @RequestBody @Validated UserRequest request) {
        userService.updateUser(UserDTO.voToDTO(request, id));
        return new SuccessResponse();
    }

    @ResponseBody
    @DeleteMapping("/{id}")
    public SuccessResponse deleteById(@PathVariable int id) {
        userService.deleteById(id);
        return new SuccessResponse();
    }

    @ResponseBody
    @GetMapping("/")
    public UserListResponse queryList(
        @RequestParam(name = "id", defaultValue = "null") @Size(min = 1, max = 10, message = "id length should between 1-10") String id,
        @RequestParam(name = "username", required = false) @Size(min = 0, max = 18, message = "username length should between 0-18") String username,
        @RequestParam(name = "current", defaultValue = Constants.DEFAULT_QUERY_CURRENT) @Min(value = 1, message = "current should >=1") int current,
        @RequestParam(name = "size", defaultValue = Constants.DEFAULT_QUERY_SIZE) @Min(value = 1, message = "size should >= 0 and <= 100") @Max(value = 100, message = "size should >= 0 and <= 100") int size) {
        IPage<UserDTO> query = new PageDTO<>();
        query.setCurrent(current);
        query.setSize(size);
        IPage<UserDTO> users = userService.selectUsers(query, RequestParamUtil.strToInt(id), username);
        CommonListResponse<UserDTO> middle = CommonListResponse.ListDTOToVO(users);
        UserListResponse response = new UserListResponse();
        BeanUtils.copyProperties(middle, response);
        response.setRecords(middle.getRecords().stream().map(UserDTO::dTOToVO).collect(Collectors.toList()));
        return response;
    }
}
